package com.hrm.system.controller;

import com.hrm.common.entity.Result;
import com.hrm.common.entity.ResultCode;
import com.hrm.domain.system.Permission;
import com.hrm.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping(value = "/sys")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 保存权限
     */
    @RequestMapping(value = "/permission", method = RequestMethod.POST)
    public Result addPermission(@RequestBody Map<String, Object> map) throws Exception {
        permissionService.addPermission(map);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 更新权限
     */
    @RequestMapping(value = "/permission/{id}", method = RequestMethod.PUT)
    public Result updatePermission(@PathVariable(value = "id") String id, @RequestBody Map<String, Object> map) throws Exception {
        map.put("id", id);
        permissionService.updatePermission(map);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * id查找权限
     */
    @RequestMapping(value = "/permission/{id}", method = RequestMethod.GET)
    public Result findPermissionById(@PathVariable(value = "id") String id) {
        Map<String, Object> map = permissionService.findPermissionById(id);
        return new Result(ResultCode.SUCCESS, map);
    }

    /**
     * 查找权限列表
     */
    @RequestMapping(value = "/permission", method = RequestMethod.GET)
    public Result findPermissions(@RequestBody Map<String, Object> map) {
        List<Permission> permissions = permissionService.findPermissions(map);
        return new Result(ResultCode.SUCCESS, permissions);
    }

    /**
     * id删除权限
     */
    @RequestMapping(value = "/permission/{id}", method = RequestMethod.DELETE)
    public Result deletePermission(@PathVariable(value = "id") String id) {
        permissionService.deletePermission(id);
        return new Result(ResultCode.SUCCESS);
    }
}
